package me.raih.controller.user;

import me.raih.pojo.po.Appointment;
import me.raih.pojo.po.User;
import me.raih.pojo.po.Wares;
import me.raih.service.AppointmentService;
import me.raih.service.UserService;
import me.raih.service.WaresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Raih
 * @Time 2021/3/12  13:03
 *
 * 用户的控制信息
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    AppointmentService appointmentService;

    @Autowired
    WaresService waresService;

    @Autowired
    UserService userService;

    @RequestMapping(value = "/myMessage")
    public String toMyMessage(HttpServletRequest request){

        User user = (User)request.getSession().getAttribute("USER_LOGIN_SESSION_FLAG");
        request.setAttribute("USER",user);
        String username = user.getUsername();
        List<Appointment> list = appointmentService.findAppointmentByUsername(username);
        List<Wares> waresList = new ArrayList<>();
        for (Appointment appointment : list) {
            String no = appointment.getNo();
            Wares wares = waresService.findWaresByNo(no);
            waresList.add(wares);
        }
        //预约的商品
        request.setAttribute("APPOINTMENT_WARES_LIST",waresList);
        //查询积分
        double point = userService.findPointsByUsername(username);
        request.setAttribute("POINTS",point);

        return "user/myMessage";
    }


    @RequestMapping(value = "/updateMyMessage",method = RequestMethod.PUT)
    public String updateMessage(User user, HttpSession session){
        User sessionUser = (User)session.getAttribute("USER_LOGIN_SESSION_FLAG");
        //给他修改后的信息，可以想一下是为什么哦
        sessionUser.setPassword(user.getPassword());
        sessionUser.setAddress(user.getAddress());
        sessionUser.setEmail(user.getEmail());
        sessionUser.setPhone(user.getPhone());
        sessionUser.setQq(user.getQq());
        sessionUser.setName(user.getName());
        userService.canUpdateUserByUser(sessionUser);
        //返回这个页面
        return "forward:/user/myMessage";
    }


}
